package com.winter.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.base.Strings;
import com.winter.mapper.AreaCodeMapper;
import com.winter.mapper.CompanyUserMapper;
import com.winter.mapper.DictEntryMapper;
import com.winter.model.AreaCode;
import com.winter.model.CompanyUser;
import com.winter.model.DictEntry;
import com.winter.util.Constants;
import com.winter.util.Result;
import com.winter.util.ResultCode;

@Service
public class CompanyUserService {

    @Autowired
    CompanyUserMapper companyUserMapper;
    
    @Autowired
    private DictEntryMapper dictEntryMapper;
    
    @Autowired
    private AreaCodeMapper areaCodeMapper;

    /**
     * 查询公司
     */
    public Result queryByUserNo(Integer pageSize, Integer pageNumber, String userNo, String companyName, Long companyId) {
        if (pageNumber == null) {
            pageNumber = Constants.PAGENUMBER;
        }
        if (pageSize == null) {
            pageSize = Constants.PAGESIZE;
        }
        if (Strings.isNullOrEmpty(userNo)) {
            return Result.failure(ResultCode.FAILURE, "用户账号不能为空");
        }
        String orderBy = "orderBy";
        //Short auditing=1;
        PageHelper.startPage(pageNumber, pageSize, false);
        List<CompanyUser> list = companyUserMapper.queryByUserNo(userNo, orderBy, companyName, companyId);
        if (list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				getTypeName(list.get(i));
			}
		}
        PageInfo<CompanyUser> pageInfo = new PageInfo<>(list);
        return Result.success(pageInfo);
    }

    /**
     * 选择职位列表
     */
    public Result queryPositionList(String userNo, String jobFunction) {
        if (Strings.isNullOrEmpty(userNo)) {
            return Result.failure(ResultCode.FAILURE, "用户id不能为空");
        }
        List<CompanyUser> list = companyUserMapper.queryPositionList(userNo, jobFunction);
        PageInfo<CompanyUser> pageInfo = new PageInfo<>(list);
        return Result.success(pageInfo);
    }
    
    public void getTypeName(CompanyUser company) {
        DictEntry dictEntry = dictEntryMapper.get("DC_INDU_CLASSIFY", company.getCompanyIndustry());
        if (dictEntry != null) {
            company.setCompanyIndustry(dictEntry.getDictname());
        }
        DictEntry dictEntry2 = dictEntryMapper.get("DC_COMPANY_SCALE", company.getCompanyScale());
        if (dictEntry2 != null) {
            company.setCompanyScale(dictEntry2.getDictname());
        }
        if (company.getComProId() != null) {
            AreaCode areaCode = areaCodeMapper.selectByPrimaryKey(company.getComProId());
            if (areaCode != null) {
                company.setComProName(areaCode.getChiShortName());
            }
        }
        if (company.getComCityId() != null) {
            AreaCode areaCode = areaCodeMapper.selectByPrimaryKey(company.getComCityId());
            if (areaCode != null) {
                company.setComCityName(areaCode.getChiShortName());
            }
        }
    }

}
